import {Injectable} from '@angular/core';
import {Room} from '../types/resp/Room';
import {RoomForm} from '../types/req/RoomForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {RoomQueryForm} from '../types/req/RoomQueryForm';

@Injectable({
  providedIn: 'root'
})
export class RoomService {
  rooms: Room[] = [
    {id: 1, buildingId: 1, buildingName: '北区1栋', roomNumber: '301', floorNumber: 3, grossFloorArea: 60.7, netFloorArea: 54.3, createTime: '2024-01-01 12:12:12', modifyTime: '2024-02-02 13:13:13'},
    {id: 2, buildingId: 2, buildingName: '北区1栋', roomNumber: '402', floorNumber: 4, grossFloorArea: 61.6, netFloorArea: 55.4, createTime: '2024-02-02 12:12:12', modifyTime: '2024-03-03 13:13:13'},
    {id: 3, buildingId: 3, buildingName: '南区1栋', roomNumber: '503', floorNumber: 5, grossFloorArea: 62.5, netFloorArea: 56.5, createTime: '2024-03-03 12:12:12', modifyTime: '2024-04-04 13:13:13'},
    {id: 4, buildingId: 4, buildingName: '南区2栋', roomNumber: '604', floorNumber: 6, grossFloorArea: 63.4, netFloorArea: 57.6, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 5, buildingId: 5, buildingName: '南区2栋', roomNumber: '504', floorNumber: 5, grossFloorArea: 62.5, netFloorArea: 55.5, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 6, buildingId: 6, buildingName: '东区1栋', roomNumber: '404', floorNumber: 4, grossFloorArea: 61.6, netFloorArea: 56.4, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 7, buildingId: 7, buildingName: '东区2栋', roomNumber: '304', floorNumber: 3, grossFloorArea: 63.7, netFloorArea: 54.3, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'}
  ]
  constructor() {
  }

  save (roomForm: RoomForm): Observable<Result<void>> {
    console.log('roomApi save params', roomForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (roomQueryForm: RoomQueryForm): Observable<Result<Page<Room>>> {
    console.log('roomApi find params', roomQueryForm)
    return MockApi.queryPageSuccessfully(this.rooms).pipe(
      map((response: Response<Result<Page<Room>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Room>> {
    console.log('roomApi findById id', id)
    let target = {}
    for (const a of this.rooms) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Room>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('roomApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
